﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMS.Models;

namespace RMS.Controllers
{
    public class BestPracticeController : Controller
    {

        RMSDataClassesDataContext db = new RMSDataClassesDataContext();

        public ActionResult Decline(int id)
        {
            return View(db.BestPractices.FirstOrDefault(x => x.Id == id));
        }
        [HttpPost]
        public ActionResult Decline(int id, string reason)
        {
            User user = db.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name);
            BestPractice bp = db.BestPractices.FirstOrDefault(x => x.Id == id);
            bp.ApprovalUserId = user.Id;
            bp.IsDeclined = true;
            bp.Reason = reason;
            db.SubmitChanges();
            return RedirectToAction("Details", "Problem", new { id = bp.ProblemId });
        }
        public ActionResult Scoring(int id, int userId,decimal score)
        {
            BestPractice bestPractice = db.BestPractices.FirstOrDefault(x => x.Id == id);
            if (bestPractice.BestPracticeScores.Any(x => x.UserId == userId))
            {
                BestPracticeScore bestPracticeScore = bestPractice.BestPracticeScores.FirstOrDefault(x => x.UserId == userId);
                bestPracticeScore.Score = score;
                db.SubmitChanges();
            }
            else
            {
                db.BestPracticeScores.InsertOnSubmit(new BestPracticeScore
                {
                    BestPracticeId = id,
                    Score = score,
                    UserId = userId
                });
                db.SubmitChanges();
            }
            return RedirectToAction("Details", "Problem", new { id = bestPractice.ProblemId });
        }
        public ActionResult AddToCompany(int id, int companyId)
        {
            db.CompaniesBestPractices.InsertOnSubmit(new CompaniesBestPractice
            {
                BestPracticeId = id,
                CompanyId = companyId
            });
            db.SubmitChanges();
            return RedirectToAction("Details", "Problem", new
            {
                id = db.BestPractices.FirstOrDefault(
                    x => x.Id == id).ProblemId
            });
        }
        public ActionResult Approve(int id)
        {

            User user = db.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name);
            BestPractice bestPractice = db.BestPractices.FirstOrDefault(x => x.Id == id);
            bestPractice.ApprovalUserId = user.Id;
            db.SubmitChanges();
            return RedirectToAction("Details", "Problem", new { id = bestPractice.ProblemId });
        }
        public ActionResult SendForApprove(int id)
        {
            BestPractice bestPractice = db.BestPractices.FirstOrDefault(x => x.Id == id);
            bestPractice.IsShared = true;
            db.SubmitChanges();
            return RedirectToAction("Details", "Problem", new { id = bestPractice.ProblemId });
        }
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Details(int id)
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /BestPractice/Create

        [HttpPost]
        public ActionResult Create(BestPractice bestPractice,decimal score,int userId)
        {
            try
            {
                
                db.BestPractices.InsertOnSubmit(bestPractice);
                db.SubmitChanges();
                bestPractice.BestPracticeScores.Add(new BestPracticeScore
                {
                    Score = score,
                    UserId = userId
                });
                db.CompaniesBestPractices.InsertOnSubmit(new CompaniesBestPractice
                {
                    BestPracticeId = bestPractice.Id,
                    CompanyId = bestPractice.CreateUser.CompanyId.Value
                });
                db.SubmitChanges();

                return RedirectToAction("Details", "Problem", new { id = bestPractice.ProblemId });
     
            }
            catch
            {
                return RedirectToAction("Details", "Problem", new { id = bestPractice.ProblemId });
            }
        }
        
        //
        // GET: /BestPractice/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /BestPractice/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /BestPractice/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /BestPractice/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
